package item

import (
	"go_server/game_server/constants"
	"go_server/game_server/dataconfig/config_manager"
	"go_server/game_server/dataconfig/configpb"
	"go_server/game_server/util"
	"strconv"
	"strings"
)

// 物品配置数据类
type Cache_Item_Config_Data struct {
	Id                     int32                           //物品id
	BagShow                constants.Const_item_bagshow    //背包分类
	Accelerate             constants.Const_item_accelerate //加速用途
	Accelerate_Millisecond int64                           //加速时间
	Quality                int32                           //品质
	HeroExp                int32                           //英雄经验
	Currency               map[int32]int32                 //货币数据
}

func (d *Cache_Item_Config_Data) set_value(cfg *configpb.StcTableItem_SheetItem_6508A6FE) {
	d.BagShow = constants.Const_item_bagshow(cfg.Bagshow)
	d.Quality = cfg.Quality
	d.set_accelerate(cfg.Accelerate)
	d.set_heroexp(cfg.Par)
	d.set_resources(cfg.Resources)
}

func (s *Cache_Item_Config_Data) set_accelerate(val string) {
	splitStr := strings.Split(val, "_")
	if len(splitStr) > 0 {
		if v, err := strconv.Atoi(splitStr[0]); err == nil {
			s.Accelerate = constants.Const_item_accelerate(v)
		}
	}

	if len(splitStr) > 1 {
		if v, err := strconv.Atoi(splitStr[1]); err == nil {
			s.Accelerate_Millisecond = int64(v * 60 * 1000)
		}
	}
}

func (s *Cache_Item_Config_Data) set_heroexp(vals []int32) {
	ok := false
	if len(vals) > 0 {
		ok = vals[0] == 1
	}

	if len(vals) > 1 {
		if ok {
			s.HeroExp = vals[1]
		}
	}
}

func (s *Cache_Item_Config_Data) set_resources(val string) {
	splitStr := strings.Split(val, "_")
	if len(splitStr) > 1 {
		id := int32(0)
		count := int32(0)
		if v0, err := strconv.Atoi(splitStr[0]); err == nil {
			id = int32(v0)
		}
		if v1, err := strconv.Atoi(splitStr[1]); err == nil {
			count = int32(v1)
		}
		if id > 0 && count > 0 {
			s.Currency = make(map[int32]int32)
			s.Currency[id] = count
		}
	}
}
func (s *Cache_Item_Config_Data) IsAccelerateBuild() bool {
	return s.Accelerate == constants.Item_Accelerate_Build
}

// ------分隔线-----------------------------------------------------------------------------------------------------------
var (
	all_item_data map[int32]*Cache_Item_Config_Data
	//map[constants.Const_item_bagshow]map[int32:物品id]*Cache_Item_Config_Data
	all_item_data_bagshow      map[constants.Const_item_bagshow]map[int32]*Cache_Item_Config_Data
	all_item_data_upgrade_hero map[constants.Const_item_upgrade_hero]map[int32]*Cache_Item_Config_Data
)

func onInitItemDataByBagshow() {
	items := config_manager.GetTableItemSheetItemMap()
	if items != nil {
		all_item_data = make(map[int32]*Cache_Item_Config_Data)
		all_item_data_bagshow = make(map[constants.Const_item_bagshow]map[int32]*Cache_Item_Config_Data)
		all_item_data_upgrade_hero = make(map[constants.Const_item_upgrade_hero]map[int32]*Cache_Item_Config_Data)
		for id, mv := range items {
			cfg := &Cache_Item_Config_Data{}
			cfg.set_value(mv)
			all_item_data[id] = cfg
			//-分隔线------------------------------------------------------------------------
			if _, k1 := all_item_data_bagshow[cfg.BagShow]; k1 {
			} else {
				all_item_data_bagshow[cfg.BagShow] = make(map[int32]*Cache_Item_Config_Data)
			}
			all_item_data_bagshow[cfg.BagShow][id] = cfg
		}

		gamesettings := config_manager.GetTableGameSettingSheetGame()
		//-分隔线------------------------------------------------------------------------
		all_item_data_upgrade_hero[constants.Item_Upgrade_HeroLv] = make(map[int32]*Cache_Item_Config_Data) //英雄升级道具
		for i := 0; i < len(gamesettings.Herouplvitems); i++ {
			id := gamesettings.Herouplvitems[i]
			all_item_data_upgrade_hero[constants.Item_Upgrade_HeroLv][id] = all_item_data[id]
		}
		//-分隔线------------------------------------------------------------------------
		all_item_data_upgrade_hero[constants.Item_Upgrade_HeroStar] = make(map[int32]*Cache_Item_Config_Data) //英雄升级道具
		for i := 0; i < len(gamesettings.Heroupstaritems); i++ {
			id := gamesettings.Heroupstaritems[i]
			all_item_data_upgrade_hero[constants.Item_Upgrade_HeroStar][id] = all_item_data[id]
		}
		//-分隔线------------------------------------------------------------------------
		all_item_data_upgrade_hero[constants.Item_Upgrade_HeroSkillLv] = make(map[int32]*Cache_Item_Config_Data) //英雄升级道具
		for i := 0; i < len(gamesettings.Heroupskillitems); i++ {
			id := gamesettings.Heroupskillitems[i]
			all_item_data_upgrade_hero[constants.Item_Upgrade_HeroSkillLv][id] = all_item_data[id]
		}
		//-分隔线------------------------------------------------------------------------
		all_item_data_upgrade_hero[constants.Item_Upgrade_HeroEquipLv] = make(map[int32]*Cache_Item_Config_Data) //英雄升级道具
		for i := 0; i < len(gamesettings.Heroupequipitems); i++ {
			id := gamesettings.Heroupequipitems[i]
			all_item_data_upgrade_hero[constants.Item_Upgrade_HeroEquipLv][id] = all_item_data[id]
		}
		//-分隔线------------------------------------------------------------------------
		all_item_data_upgrade_hero[constants.Item_Upgrade_HerocCrrencyFragments] = make(map[int32]*Cache_Item_Config_Data) //英雄升级道具
		for i := 0; i < len(gamesettings.Herocurrencyfragments); i++ {
			id := gamesettings.Herocurrencyfragments[i]
			all_item_data_upgrade_hero[constants.Item_Upgrade_HerocCrrencyFragments][id] = all_item_data[id]
		}
		//-分隔线------------------------------------------------------------------------
	}
}

// ------分隔线-----------------------------------------------------------------------------------------------------------
// 按背包分类查询物品
func GetItemDataByBagShow(bagshow constants.Const_item_bagshow, quality int32) (map[int32]*Cache_Item_Config_Data, bool) {
	onInitItemDataByBagshow()
	rstData := make(map[int32]*Cache_Item_Config_Data)
	if val, k1 := all_item_data_bagshow[bagshow]; k1 {
		for id, data := range val {
			if data.Quality == quality || quality < 0 {
				rstData[id] = data
			}
		}
		return rstData, true
	}
	return rstData, false
}

func GetItemData(itemid int32) (*Cache_Item_Config_Data, bool) {
	onInitItemDataByBagshow()
	val, ok := all_item_data[itemid]
	return val, ok
}

func GetUpgradeHeroItems(upgrade constants.Const_item_upgrade_hero, quality int32) (map[int32]*Cache_Item_Config_Data, bool) {
	onInitItemDataByBagshow()
	rstData := make(map[int32]*Cache_Item_Config_Data)
	if val, ok := all_item_data_upgrade_hero[upgrade]; ok {
		for id, data := range val {
			if data.Quality == quality || quality < 0 {
				rstData[id] = data
			}
		}
		return rstData, ok
	}
	return rstData, false
}

// 获得机器人补充体力道具
func GetRobotStrengthItems() []int32 {
	rstItems := util.NewSet[int32]()
	rstItems.AddValue(config_manager.GetTableGameSettingSheetGame().RobotStrength)
	return rstItems.Elements()
}
